package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.SpecialAttendanceRuleBo;
import org.eiahe.hr.attendance.domain.vo.SpecialAttendanceRuleVo;
import org.eiahe.hr.attendance.service.ISpecialAttendanceRuleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 特殊员工考勤规则管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/specialRule")
public class SpecialAttendanceRuleController extends BaseController {

    private final ISpecialAttendanceRuleService specialAttendanceRuleService;

    /**
     * 查询特殊员工考勤规则列表
     */
    @SaCheckPermission("attendance:specialRule:list")
    @GetMapping("/list")
    public TableDataInfo<SpecialAttendanceRuleVo> list(SpecialAttendanceRuleBo bo, PageQuery pageQuery) {
        return specialAttendanceRuleService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取特殊员工考勤规则详细信息
     */
    @SaCheckPermission("attendance:specialRule:query")
    @GetMapping("/{ruleId}")
    public R<SpecialAttendanceRuleVo> getInfo(@PathVariable Long ruleId) {
        return R.ok(specialAttendanceRuleService.queryById(ruleId));
    }

    /**
     * 新增特殊员工考勤规则(提交申请)
     */
    @SaCheckPermission("attendance:specialRule:add")
    @Log(title = "提交特殊考勤规则申请", linkType = "attendance", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SpecialAttendanceRuleBo bo) {
        return toAjax(specialAttendanceRuleService.insertByBo(bo));
    }

    /**
     * 修改特殊员工考勤规则
     */
    @SaCheckPermission("attendance:specialRule:edit")
    @Log(title = "修改特殊考勤规则", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SpecialAttendanceRuleBo bo) {
        return toAjax(specialAttendanceRuleService.updateByBo(bo));
    }

    /**
     * 删除特殊员工考勤规则
     */
    @SaCheckPermission("attendance:specialRule:remove")
    @Log(title = "删除特殊考勤规则", linkType = "attendance", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ruleIds}")
    public R<Void> remove(@PathVariable Long[] ruleIds) {
        return toAjax(specialAttendanceRuleService.deleteWithValidByIds(List.of(ruleIds)));
    }

    /**
     * 审批通过
     */
    @SaCheckPermission("attendance:specialRule:approve")
    @Log(title = "审批通过特殊考勤规则", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PostMapping("/approve/{ruleId}")
    public R<Void> approve(@PathVariable Long ruleId, @RequestParam(required = false) String approvalComment) {
        return toAjax(specialAttendanceRuleService.approve(ruleId, approvalComment));
    }

    /**
     * 审批驳回
     */
    @SaCheckPermission("attendance:specialRule:approve")
    @Log(title = "审批驳回特殊考勤规则", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{ruleId}")
    public R<Void> reject(@PathVariable Long ruleId, @RequestParam(required = false) String approvalComment) {
        return toAjax(specialAttendanceRuleService.reject(ruleId, approvalComment));
    }

    /**
     * 撤销申请
     */
    @SaCheckPermission("attendance:specialRule:cancel")
    @Log(title = "撤销特殊考勤规则申请", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel/{ruleId}")
    public R<Void> cancel(@PathVariable Long ruleId) {
        return toAjax(specialAttendanceRuleService.cancel(ruleId));
    }

    /**
     * 导出特殊员工考勤规则列表
     */
    @SaCheckPermission("attendance:specialRule:export")
    @Log(title = "导出特殊考勤规则", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SpecialAttendanceRuleBo bo, HttpServletResponse response) {
        List<SpecialAttendanceRuleVo> list = specialAttendanceRuleService.queryList(bo);
        ExcelUtil.exportExcel(list, "特殊考勤规则数据", SpecialAttendanceRuleVo.class, response);
    }
}
